<?php

namespace addons\mental\controller;

use addons\mental\model\Config;
use fast\Random;
use think\Config as tConfig;
use think\Hook;

class Common extends Base
{
     protected $noNeedLogin = ['config'];
    /**
     * Undocumented function
     * 获取公共的配置
     * @return void
     */
    public function config()
    {
        $data = Config::getVal();
        $data['hotkeyword'] = explode(',', $data['hotkeyword']);
        $data['shuffling_figure'] = explode(',', $data['shuffling_figure']);
        $data['shuffling_content'] = json_decode($data['shuffling_content'], true);
        $cdnurl = tConfig::get('upload.cdnurl');
        foreach ($data['shuffling_content'] as $key=>&$val) {
            if(!empty($cdnurl)){                
                $val['url'] = $cdnurl . $data['shuffling_figure'][$key];
            }else{
                $val['url'] = $this->request->domain() . $data['shuffling_figure'][$key];
            }
        }
        
        if(!empty($cdnurl)){
            $data['logo'] = $cdnurl . $data['logo'];
        }else{
            $data['logo'] = $this->request->domain() . $data['logo'];
        } 
        
        //配置信息
        $upload = tConfig::get('upload');

        //如果非服务端中转模式需要修改为中转
        if ($upload['storage'] != 'local' && isset($upload['uploadmode']) && $upload['uploadmode'] != 'server') {
            //临时修改上传模式为服务端中转
            set_addon_config($upload['storage'], ["uploadmode" => "server"], false);

            $upload = \app\common\model\Config::upload();
            // 上传信息配置后
            Hook::listen("upload_config_init", $upload);

            $upload = tConfig::set('upload', array_merge(tConfig::get('upload'), $upload));
        }

        $upload['cdnurl'] = $upload['cdnurl'] ? $upload['cdnurl'] : cdnurl('', true);
        $upload['uploadurl'] = preg_match("/^((?:[a-z]+:)?\/\/)(.*)/i", $upload['uploadurl']) ? $upload['uploadurl'] : url($upload['storage'] == 'local' ? '/api/common/upload' : $upload['uploadurl'], '', false, true);
        $data['upload'] = $upload;
        unset($val);
        unset($data['shuffling_figure']);
        $this->success(__('Operation completed'), $data);
    }
}
